package tools

import (
	"fmt"
	"github.com/pkg/errors"
	"github.com/spf13/cast"
	"regexp"
	"strings"
)

func String2Int64s(id string) []int64 {
	idArr := make([]int64, 0)
	for _, v := range strings.Split(id, ",") {
		idArr = append(idArr, cast.ToInt64(v))
	}

	return idArr
}

func CheckPasswordLever(ps string, min, max int) error {
	if len(ps) < min {
		return fmt.Errorf("密码长度小于%d位", min)
	}

	if len(ps) > max {
		return fmt.Errorf("密码长度最大%d位", max)
	}

	num := `[0-9]{1}`
	a_z := `[a-z]{1}`
	A_Z := `[A-Z]{1}`
	symbol := `[!@#~$%^&*()+|_]{1}`
	if b, err := regexp.MatchString(num, ps); !b || err != nil {
		return errors.New("密码必须含有数字")
	}
	if b, err := regexp.MatchString(a_z, ps); !b || err != nil {
		return errors.New("密码必须含有小写字母")
	}
	if b, err := regexp.MatchString(A_Z, ps); !b || err != nil {
		return errors.New("密码必须含有大写字母")
	}

	if b, err := regexp.MatchString(symbol, ps); !b || err != nil {
		return errors.Errorf("密码必须含有特殊字符，字符范围：%s", "!@#~$%^&*()+|_")
	}

	return nil
}
